home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-08-07 | 14.5 KB | 675 lines | [TEXT/MPS ] |
- PROGRAM cdevShell;
-
- USES MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf,
- cdev;
-
- CONST
- cursorDev = 14;
- HwCfgFlgs = $0B22;
- ROM85 = $028E;
-
- TYPE
- IntPtr = ^integer;
- IntHandle = ^IntPtr;
- RectPtr = ^rect;
- EventPtr = ^EventRecord;
-
- PROCEDURE doMenu(l: longInt); FORWARD;
- PROCEDURE updateDial; FORWARD;
- PROCEDURE drawIcon(ih: Handle; selected: Boolean; r: Rect); FORWARD;
- PROCEDURE startupCdev; FORWARD;
- PROCEDURE shutDownCdev; FORWARD;
- PROCEDURE cdevInitDev; FORWARD;
- PROCEDURE cdevHitDev(item: integer; e: EventPtr); FORWARD;
- PROCEDURE cdevUpdateDev; FORWARD;
- PROCEDURE cdevNulDev(e: EventPtr); FORWARD;
- PROCEDURE cdevCloseDev; FORWARD;
- PROCEDURE cdevEdit(c: integer); FORWARD;
- PROCEDURE cdevKeyEvtDev(e: EventPtr); FORWARD;
- PROCEDURE cdevActivate(e: EventPtr); FORWARD;
- PROCEDURE cdevMacDev; FORWARD;
- PROCEDURE cdevCursorDev; FORWARD;
- PROCEDURE fatalError(s : Str255); FORWARD;
- PROCEDURE warningError(s : Str255); FORWARD;
- PROCEDURE doMsgDialog; FORWARD;
- FUNCTION callCdev( msgCode : integer; item : integer; e : EventPtr;
- st : longInt) : longInt; FORWARD;
- PROCEDURE doNRCTs; FORWARD;
- PROCEDURE getNRCTrgn; FORWARD;
- PROCEDURE HiliteDefault(d: DialogPtr); FORWARD;
-
- VAR
- cdevDial: DialogPtr;
- dP: DialogPeek;
- cdevItems: integer;
- userItems: integer;
- teWas: integer;
- cdevStorage: longInt;
-
- nrctRgn: RgnHandle;
- greyRgn: RgnHandle;
- userArea: Rect;
-
- stopRect,
- startRect: Rect;
- myStopIcon,
- myStartIcon: Handle;
-
- cursorIsArrow: Boolean;
- cdevMsgOff: array[0 .. 16] of Boolean;
- selected: Boolean;
- running: Boolean;
-
- env: SysEnvRec;
- entryVref: integer;
- otherWindow: WindowPtr;
- error: integer;
-
- { ******* }
- i,j: integer;
- temp, cdevDITL: Handle;
- e: EventRecord;
- rH: RgnHandle;
- whichWindow: WindowPtr;
- key: char;
- p: Point;
- s: Str255;
- hasCursor: Boolean;
- teH: TEHandle;
- trashB: Boolean;
-
- PROCEDURE doMenu(l: longInt);
- VAR menu, choice, i: integer;
- daName: Str255;
- d: DialogPtr;
- BEGIN
- menu := HiWord(l);
- choice := LoWord(l);
-
- case menu of
- 1: BEGIN
- if choice = 1 then BEGIN
- d := GetNewDialog(132, nil, WindowPtr(-1));
- HiliteDefault(d);
- repeat
- ModalDialog(nil, i)
- until i = 1;
- DisposDialog(d);
- END;
- if choice > 1 then BEGIN
- GetItem(GetMHandle(1), choice, daName);
- error := OpenDeskAcc(daName);
- END;
- END;
- 2: case choice of
- 1: startupCdev;
- 2: shutDownCdev;
- 4: running := false;
- END;
- 3: if FrontWindow = cdevDial then
- cdevEdit(choice)
- else
- trashB := SystemEdit(choice - 1);
- 4: case choice of
- 1: doMsgDialog;
- 2: InvalRect(GrafPtr(cdevDial)^.portRect);
- END; { of case }
- END; { of case }
- HiliteMenu(0);
- END;
-
- PROCEDURE updateDial;
- VAR
- r: Rect;
- patHand: PatHandle;
- i: integer;
- h: Handle;
- BEGIN
- SetPort(cdevDial);
- TextSize(9);
-
- PenSize(2,2);
-
- patHand := GetPattern(-16000);
- PenPat(patHand^^);
- PaintRgn(nrctRgn);
- PenPat(black);
-
- doNRCTs;
- r := cdevDial^.portRect;
- InsetRect(r, -2, -2);
- r.top := r.top + 1;
- FrameRect(r);
-
- GetDItem(cdevDial, 2, i, h, r);
- r.top := r.top - 2;
- r.bottom := r.bottom -1;
- r.left := r.left - 2;
- r.right := r.right + 1;
- FrameRect(r);
-
- GetDItem(cdevDial, 1, i, h, r);
- r.top := r.top - 2;
- r.bottom := r.bottom + 2;
- r.left := r.left - 2;
- r.right := r.right + 2;
- FrameRect(r);
-
- PenSize(1,1);
-
- drawIcon(myStopIcon, not selected, stopRect);
- drawIcon(myStartIcon, selected, startRect);
- END;
-
- PROCEDURE drawIcon(ih: Handle; selected: Boolean; r: Rect);
- VAR
- iconBitMap: BitMap;
- fromR: Rect;
- BEGIN
- SetRect(fromR, 0, 0, 32, 32);
- iconBitMap.rowBytes := 4;
- iconBitMap.bounds := fromR;
- HLock(iH);
- if (selected) then BEGIN
- iconBitMap.baseAddr := Ptr(ORD4(iH^) + longInt(128));
- CopyBits(iconBitMap, GrafPtr(cdevDial)^.portBits, fromR, r, srcCopy, nil);
-
- iconBitMap.baseAddr := ih^;
- CopyBits(iconBitMap, GrafPtr(cdevDial)^.portBits, fromR, r, srcXor, nil);
- END
- else BEGIN
- iconBitMap.baseAddr := ih^;
- CopyBits(iconBitMap, GrafPtr(cdevDial)^.portBits, fromR, r, srcCopy, nil);
- END;
- HUnlock(iH);
- END;
-
- PROCEDURE startupCdev;
- VAR
- temp: Handle;
- i,j,totalItems: integer;
- hardMask, softMask: integer;
- p: Ptr;
- c: ControlHandle;
- tempD: DialogPtr;
- r: Rect;
- BEGIN
- selected := true;
-
- getNRCTrgn;
-
- temp := GetResource('mach', -4064);
- if temp = nil then
- fatalError('mach -4064 resource not found');
- softMask := IntHandle(temp)^^;
- hardMask := IntPtr(2+ORD4(temp^))^;
- if (softMask = 0) and (hardMask = $ffff) then BEGIN
- cdevMacDev;
- if cdevStorage = 0 then
- fatalError('cdev refuses to run on this machine');
- if cdevStorage <> 1 then
- fatalError('Invalid result returned from macDev call');
- END
- else BEGIN
- if integer(BitAnd(softMask, IntPtr(ROM85)^)) <> Intptr(ROM85)^ then
- fatalError('cdev failed SoftMask test');
- if integer(BitAnd(hardMask, IntPtr(HwCfgFlgs)^)) <> hardMask then
- fatalError('cdev failed HardMask test');
- END;
- ReleaseResource(temp);
-
- IntHandle(dP^.items)^^ := cdevItems + userItems;
- dP^.editField := teWas;
- if teWas <> -1 then
- TEActivate( dP^.textH);
- SetPort(cdevDial);
- TextSize(9);
- for i:= cdevItems + 1 to cdevItems + userItems do
- ShowDItem(cdevDial,i);
-
- doNRCTs;
-
- DisableItem(GetMHandle(2),1);
- EnableItem(GetMHandle(2), 2);
-
- cdevInitDev;
-
- InvalRect(GrafPtr(cdevDial)^.portRect);
- END;
-
- PROCEDURE shutDownCdev;
- VAR i: integer;
- BEGIN
- cdevCloseDev;
-
- SetCursor(arrow);
- cursorIsArrow := true;
-
- if cdevStorage <> 0 then
- warningError('Close call did not return zero');
-
- for i := cdevItems + 1 to cdevItems + userItems do
- HideDItem(cdevDial, i);
-
- IntHandle(dP^.items)^^ := cdevItems;
- dP^.editField := -1;
- if teWas <> -1 then
- TEDeactivate(dP^.textH);
-
- selected := false;
-
- getNRCTrgn;
-
- EnableItem(GetMHandle(2), 1);
- DisableItem(GetMHandle(2), 2);
-
- InvalRect(GrafPtr(cdevDial)^.portRect);
- END;
-
- PROCEDURE cdevInitDev;
- BEGIN
- cdevStorage := callCdev(initDev, 0, nil, cdevUnset);
- END;
-
- PROCEDURE cdevHitDev(item: integer; e: EventPtr);
- BEGIN
- cdevStorage := callCdev(hitDev, item, e, cdevStorage);
- END;
-
- PROCEDURE cdevUpdateDev;
- BEGIN
- cdevStorage := callCdev(updateDev, 0, nil, cdevStorage);
- END;
-
- PROCEDURE cdevNulDev(e: EventPtr);
- BEGIN
- cdevStorage := callCdev(nulDev, 0, e, cdevStorage);
- END;
-
- PROCEDURE cdevCloseDev;
- BEGIN
- cdevStorage := callCdev(closeDev, 0, nil, cdevStorage);
- END;
-
- PROCEDURE cdevEdit(c: integer);
- BEGIN
- case c of
- 1: i := undoDev;
- 3: i := cutDev;
- 4: i := copyDev;
- 5: i := pasteDev;
- 6: i := clearDev;
- END;
- cdevStorage := callCdev(i, 0, nil, cdevStorage);
- END;
-
- PROCEDURE cdevKeyEvtDev(e: EventPtr);
- BEGIN
- cdevStorage := callCdev(keyEvtDev, 0, e, cdevStorage);
- END;
-
- PROCEDURE cdevActivate(e: EventPtr);
- VAR i: integer;
- BEGIN
- if BitAnd(e^.modifiers, activeFlag) <> 0 then
- i := activDev
- else
- i := deactivDev;
- cdevStorage := callCdev(i, 0, e, cdevStorage);
- END;
-
- PROCEDURE cdevMacDev;
- BEGIN
- cdevStorage := callCdev(macDev, 0, nil, cdevStorage);
- END;
-
- PROCEDURE cdevCursorDev;
- BEGIN
- cdevStorage := callCdev(cursorDev, 0, nil, cdevStorage);
- END;
-
- FUNCTION callCdev( msgCode : integer; item : integer; e : EventPtr;
- st : longInt) : longInt;
- var rValue: longInt;
- error : integer;
- BEGIN
- if (not selected) or (cdevMsgOff[msgCode]) then BEGIN
- callCdev := st;
- exit(callCdev);
- END;
-
- SetPort(cdevDial);
- TextSize(9);
- error := SetVol(nil, env.sysVRefNum);
- rValue := Sample(msgCode, item, cdevItems, 0, e^, st, cdevDial);
- error := SetVol(nil, entryVref);
-
- if (msgCode <> macDev) and (msgCode <> closeDev) then
- case rValue of
- cdevGenErr: fatalError('General cdev error');
- cdevMemErr: fatalError('cdev insufficient memory error');
- cdevResErr: fatalError('cdev could not locate needed resource');
- END; { of case }
- callCdev := rValue;
- END;
-
- PROCEDURE fatalError(s : Str255);
- BEGIN
- ParamText(s, '', '', '');
- error := Alert(130, nil);
- ExitToShell;
- END;
-
- PROCEDURE warningError(s : Str255);
- BEGIN
- ParamText(s, '', '', '');
- error := Alert(131, nil);
- END;
-
- PROCEDURE doMsgDialog;
- VAR d: DialogPtr;
- i,kind: integer;
- r: Rect;
- h: Handle;
- BEGIN
- d := GetNewDialog(128, nil, WindowPtr(-1));
- HiliteDefault(d);
-
- for i:= initDev + 3 to cursorDev + 3 do BEGIN
- GetDItem(d, i, kind, h, r);
- kind := integer(cdevMsgOff[i-3]);
- if kind <> 0 then
- kind := 1;
- SetCtlValue(ControlHandle(h), kind);
- END;
-
- repeat
- ModalDialog(nil, i);
- if i > 2 then BEGIN
- GetDItem(d, i, kind, h, r);
- SetCtlValue(ControlHandle(h), ABS(GetCtlValue(ControlHandle(h)) - 1));
- END
- until i <= 2;
-
- if i = 1 then BEGIN
- for i:= initDev + 3 to cursorDev + 3 do BEGIN
- GetDItem(d, i, kind, h, r);
- cdevMsgOff[i-3] := Boolean(GetCtlValue(ControlHandle(h)));
- END;
- h := GetResource('msks', 128);
- if h <> nil then BEGIN
- BlockMove(Ptr(@cdevMsgOff), h^, 16);
- ChangedResource(h);
- WriteResource(h);
- END;
- END;
-
- DisposDialog(d);
- END;
-
- PROCEDURE doNRCTs;
- VAR h: Handle;
- r: RectPtr;
- i: integer;
- BEGIN
- if not selected then
- exit(doNRCTs);
-
- h := GetResource('nrct', -4064);
- r := RectPtr(2 + ORD4(h^));
- for i:= 1 to IntHandle(h)^^ do BEGIN
- EraseRect(r^);
- FrameRect(r^);
- r := RectPtr(ORD4(r) + sizeof(Rect));
- END; { of loop }
- ReleaseResource(h);
- END;
-
- PROCEDURE getNRCTrgn;
- VAR
- h: Handle;
- r: RectPtr;
- rct: Rect;
- i: integer;
- rRgn: RgnHandle;
- BEGIN
- if not selected then
- BEGIN
- SetRect(rct, 86, -1, 322, 255);
- nrctRgn := NewRgn;
- RectRgn(nrctRgn, rct);
- exit(getNRCTrgn);
- END;
-
- h := GetResource('nrct', -4064);
- if h = nil then
- fatalError('nrct -4064 resource is missing');
- nrctRgn := NewRgn;
- HLock(h);
- r := RectPtr(2+ORD4(h^));
- for i:= 1 to IntHandle(h)^^ do BEGIN
- rRgn := NewRgn;
- RectRgn(rRgn, r^);
- r := RectPtr(ORD4(r) + sizeof(Rect));
- UnionRgn(nrctRgn, rRgn, nrctRgn);
- DisposeRgn(rRgn);
- END; { of loop }
- HUnlock(h);
- ReleaseResource(h);
-
- SetRect(rct, 86, -1, 322, 255);
- rRgn := NewRgn;
- RectRgn(rRgn, rct);
- DiffRgn(rRgn, nrctRgn, nrctRgn);
- DisposeRgn(rRgn);
- END;
-
- PROCEDURE mvWindow(w: WindowPtr; p: Point);
- VAR trashR: Rect;
- BEGIN
- trashR := screenBits.bounds;
- InsetRect(trashR, 6, 6);
- DragWindow(w, p, trashR);
- END;
-
- PROCEDURE HiliteDefault(d: DialogPtr);
- VAR i: integer;
- box: Rect;
- h: Handle;
- BEGIN
- SetPort(d);
- GetDItem(d, 1, i, h, box);
- PenSize(3,3);
- InsetRect(box, -4, -4);
- FrameRoundRect(box, 16, 16);
- END;
-
- PROCEDURE Initialize;
- BEGIN
- InitGraf(@thePort);
- InitFonts;
- InitWindows;
- TEInit;
- InitDialogs(nil);
- InitCursor;
- cursorIsArrow := true;
- MaxApplZone;
-
- SetMenuBar( GetNewMBar(256) );
- AddResMenu( GetMHandle(1), 'DRVR');
- DrawMenuBar;
- DisableItem(GetMHandle(2),2);
-
- error := SysEnvirons(0, env);
- error := GetVol(@s, entryVref);
-
- getNRCTrgn;
-
- temp := GetResource('msks', 128);
- if temp <> nil then
- BlockMove(temp^, Ptr(@cdevMsgOff), 16);
-
- hasCursor := ( GetResource('CURS', -4064) <> nil );
-
- SetRect(userArea, 87, 0, 322, 255);
- SetRect(stopRect, 25, 15, 25+32, 15+32);
- SetRect(startRect, 25, 85, 25+32, 85+32);
-
- myStartIcon := GetResource('ICN#', -4064);
- if (myStartIcon = nil) then
- fatalError('Unable to find cdev ICN# -4064 resource');
- myStopIcon := GetResource('ICN#', 128);
-
- selected := false;
-
- cdevDITL := GetResource('DITL', -16000);
- HNoPurge(cdevDITL);
- temp := GetResource('DITL', -4064);
- if temp = nil then
- fatalError('Unable to find cdev DITL -4064 resource');
- cdevItems := 1 + IntHandle(cdevDITL)^^;
-
- userItems := IntHandle(temp)^^;
- IntHandle(cdevDITL)^^ := cdevItems + userItems;
- i := GetHandleSize( cdevDITL );
- j := GetHandleSize( temp );
- SetHandleSize(cdevDITL, i+j-2);
- BlockMove(Ptr(2 + ORD4(temp^)), Ptr(i + ORD4(cdevDITL^)), j-2);
-
- SetDAFont(1);
- cdevDial := GetNewDialog(-16000, nil, WindowPtr(-1));
- SetDAFont(0);
- dP := DialogPeek(cdevDial);
- SetPort(cdevDial);
- TextSize(9);
- ShowWindow(cdevDial);
- for i:= cdevItems + 1 to cdevItems+userItems do
- HideDItem(cdevDial, i);
-
- IntHandle(dP^.items)^^ := cdevItems;
- teWas := dP^.editField;
- dP^.editField := -1;
- if teWas >= 0 then BEGIN
- teH := dp^.textH;
- teH^^.txSize := 9;
- END;
-
- otherWindow := GetNewWindow(128, nil, WindowPtr(0));
- END; { of Initialize}
-
- PROCEDURE DoNullEvent;
- BEGIN
- cdevNulDev(@e);
- SetPort(cdevDial);
- GetMouse(p);
- if (FrontWindow = cdevDial) and PtInRect(p, userArea) then BEGIN
- if hasCursor then
- cdevCursorDev
- else
- if cursorIsArrow then
- SetCursor(GetCursor(crossCursor)^^);
- cursorIsArrow := false;
- END
- else
- if not cursorIsArrow then BEGIN
- SetCursor(arrow);
- cursorIsArrow := true
- END
- END; { of doNullEvent }
-
- PROCEDURE DoMouseDown;
- BEGIN
- i := FindWindow(e.where, whichWindow);
- case i of
- inMenuBar: doMenu( MenuSelect(e.where) );
- inDrag: BEGIN
- mvWindow(whichWindow, e.where);
- e.what := nullEvent;
- END;
- inGoAway: if TrackGoAway(whichWindow, e.where) then
- running := false;
- inSysWindow: SystemClick(e, whichWindow);
- inContent: BEGIN
- if FrontWindow <> whichWindow then BEGIN
- SelectWindow(whichWindow);
- e.what := nullEvent
- END
- else if (FrontWindow = cdevDial) then BEGIN
- p := e.where;
- GlobalToLocal(p);
- if (not selected) and PtInRect(p, startRect) then
- startUpCdev;
- if selected and PtInRect(p, stopRect) then
- shutDownCdev;
- END; { of if frontWindow}
- END; { of inContent}
- END; { end of case }
- END;
-
- { the Main procedure }
- BEGIN
- Initialize;
-
- running := true;
- while running do BEGIN
- SystemTask;
-
- if dp^.editField >= 0 then
- TEIdle( dp^.textH);
-
- if GetNextEvent(everyEvent, e) then BEGIN
-
- if (e.what = keyDown) or (e.what = autoKey) then BEGIN
- key := char(BitAnd(e.message,charCodeMask));
- if BitAnd(e.modifiers, cmdKey) <> 0 then BEGIN
- doMenu(MenuKey(key));
- e.what := nullEvent;
- END;
- END;
-
- if e.what = mouseDown then
- DoMouseDown;
-
- if (e.what = updateEvt) and (e.message=longInt(otherWindow))
- then BEGIN
- BeginUpdate(otherWindow);
- EndUpdate(otherWindow);
- END;
- if IsDialogEvent(e) then BEGIN
- if (e.what = updateEvt) and (e.message = longInt(cdevDial))
- then BEGIN
- rH := WindowPeek(cdevDial)^.updateRgn;
- error := HandToHand(Handle(rH));
- BeginUpdate(cdevDial);
- updateDial;
- cdevUpdateDev;
- EndUpdate(cdevDial);
- WindowPeek(cdevDial)^.updateRgn := rH;
- END;
- if (e.what = keyDown) or (e.what = autoKey) then
- cdevKeyEvtDev(@e);
- if e.what = activateEvt then
- cdevActivate(@e);
-
- SetPort(cdevDial);
- TextSize(9);
- if DialogSelect(e, cdevDial, i) then BEGIN
- if e.what = mouseDown then BEGIN
- if i =1 then
- error := Alert(-16000, nil);
- if i > cdevItems then
- cdevHitDev(i, @e);
- END;
- END; { of DialogSelect if }
- END; { of DialogEvent if }
- END; { end of GNE if }
- if e.what = nullEvent then
- doNullEvent;
-
- END;
-
- cdevCloseDev;
- DisposDialog(cdevDial);
- DisposeWindow(otherWindow);
- END.
-